/* See LICENSE for copyright and terms of use */

import org.evtmng.*;
import org.evtmng.events.*;
import org.evtmng.timeline.*;
import org.evtmng.tween.*;

/**
 * @author Tay Ray Chuan
 */

class org.evtmng.test.FadeBoxUtil {
	private static var WIDTH:Number = 20;
	private static var HEIGHT:Number = 20;
	private static var X_SEP:Number = 5;
	private static var Y_SEP:Number = 5;
	private static var X_NUM:Number = 5;
	private static var Y_NUM:Number = 5;

	private static var DURATION:Number = 150;
	private static var PAD:Number = 50;
	private static var INTERVAL:Number = 10;

	private var m_mc:MovieClip;
	private var m_mcs:Array;

	private var m_in_txt:TextField;

	/**
	 * Equivalent to Quad.easeInOut
	 */
	private static function EASE(t:Number, b:Number, c:Number, d:Number):Number {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
	
	/**
	 * Clears list of clips in case it isn't empty (legacy)
	 * @param	mc:MovieClip Parent clip to attach boxes to.
	 */
	public function init(mc:MovieClip):Void {
		m_mc = mc;
		
		var i:Number = m_mcs.length;
		while(i--) {
			MovieClip(m_mcs[i]).removeMovieClip();
			m_mcs[i] = null;
		}

		m_mcs = initClips();
	}
	
	/**
	 * Helper function to create each clip.
	 * @return List of clips
	 */
	private function initClips():Array {
		var arr:Array = [];
		var mc:MovieClip;
		var x:Number, y:Number;
		var p_x:Number, p_y:Number;

		var sum:Number;

		p_x = X_NUM+1;
		p_y = Y_NUM+1;

		for (x=p_x-1;x;x--) {
			for (y=p_y-1;y;y--) {
				mc = m_mc.createEmptyMovieClip("box_"+x+"_"+y, m_mc.getNextHighestDepth());
				mc._x = x*(WIDTH + X_SEP);
				mc._y = y*(HEIGHT+ Y_SEP);
				drawClip(mc);

				sum = (x + y)*PAD;

				if(arr[sum]==null) {
					arr[sum] = [];
				}
				arr[sum].push(mc);

				handleTweenAddition(sum, mc);
			}
		}
		return arr;
	}
	
	/**
	 * Draws a green box with a red outline.
	 * @param	mc Clip to drawn on.
	 */
	private function drawClip(mc:MovieClip):Void {
		var x:Number, y:Number, w:Number, h:Number;
		x = y = 0;
		w = WIDTH;
		h = HEIGHT;
		with(mc) {
			lineStyle(0, 0xFF0000, 100);
			beginFill(0x00FF00, 100);
			moveTo(x, y);
			lineTo(x+w, y);
			lineTo(x+w, y+h);
			lineTo(x, y+h);
			lineTo(x, y);
			endFill();
		}
	}
	
	/**
	 * Called after the clip is drawn.
	 * @param	sum Distance of clip from origin. (non-unique to a clip)
	 * @param	mc The clip at that distance.
	 */
	private function handleTweenAddition(sum:Number, mc:MovieClip):Void {
		//override
	}
	
	/**
	 * Starts animation sequence.
	 */
	public function run():Void {
		//override
	}
}
